package com.dream.inter.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder.In;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import sun.org.mozilla.javascript.internal.annotations.JSConstructor;

import com.dream.common.Common;
import com.dream.common.HttpErrorException;
import com.dream.common.JsonString;
import com.dream.common.Msg;
import com.dream.common.QQImage;
import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.model.Sys_Class;
import com.dream.model.Sys_ClassPlantMaster;
import com.dream.model.Sys_File;
import com.dream.model.Sys_Lable;
import com.dream.model.Sys_LearnPlant;
import com.dream.model.Sys_Question;
import com.dream.model.Sys_Room;
import com.dream.model.Sys_TestPaper;
import com.dream.system.service.ISysClass;
import com.dream.system.service.ISysClassPlant;
import com.dream.system.service.ISysLable;
import com.dream.system.service.ISysUser;
import com.dream.system.service.ISys_LearnPlant;
import com.dream.system.service.ISys_TestPaper;
import com.dream.system.service.TellValidateService;
import com.qcloud.sign.FileCloudSign;

@Controller
@RequestMapping({ "/inter/loginAction.do" })
public class LoginActionInter extends SuperController {

	@Autowired
	private ISysClassPlant sysClassPlantImpl;
	@Autowired
	private ISysUser sysUserImpl;
	@Autowired
	private TellValidateService tellValidateServiceImpl;

	@Autowired
	private ISys_TestPaper sys_TestPaperServiceImpl;

	@Autowired
	private ISysLable sysLableService;

	@Autowired
	private ISysClass sysClassServiceImpl;

	@RequestMapping(params = { "method=sign" })
	public void sign(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		int appId = Integer.parseInt(request.getParameter("appid"));
		String secret_id = request.getParameter("secret_id");
		String secret_key = request.getParameter("secret_key");
		String bucket = request.getParameter("bucket");
		String fileid = request.getParameter("fileid");
		String str = FileCloudSign.appSignOnceV2(appId, secret_id, secret_key, bucket, fileid);
		response.getWriter().write(str);
		response.getWriter().close();
		response.getWriter().flush();
	}

	@RequestMapping(params = { "method=login" })
	public void login(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Object bl = isAdmin(model.getTell(), model.getUserPwd());
		response.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		if ((model.getTell() == null) || ("".equals(model.getTell()))) {
			Msg msg = getMsg("0", "用户不能为空!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if ((model.getUserPwd() == null) || ("".equals(model.getUserPwd()))) {
			Msg msg = getMsg("0", "密码不能为空!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if ((model.getUserType() == null) || ("".equals(model.getUserType()))) {
			Msg msg = getMsg("0", "用户类型不能为空!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		if (bl != null) {
			SysUser su = (SysUser) bl;
			su.setUserType("6");
			request.getSession().setAttribute("sysUser", su);
			Msg msg = getMsg("1", "用户名或密码不正确!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		int temp = this.sysUserImpl.getUserlogin(model, request);
		if (temp == 1) {
			Msg msg = getMsg("1", "登陆成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	@RequestMapping(params = { "method=loginUrl" })
	public String loginUrl(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser su = (SysUser) request.getSession().getAttribute("sysUser");
		if (null == su) {
			return "/login";
		}
		if ("2".equals(su.getUserType())) {
			return "/teacherIndex";
		}
		if ("5".equals(su.getUserType())) {
			return "/zhuanjia";
		}
		if ("1".equals(su.getUserType())) {
			return "/studentIndex";
		}
		return "/index";
	}

	/**
	 * 发送短信
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = { "method=sendMessage" })
	public void sendMessage(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String code = Common.createRandom(4);
		try {
			response.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			String tellId = request.getParameter("tellId");
			String str = new com.dream.common.UcpaasClientService().sendSMS(tellId, code);
			System.out.println("发送消息的结果为：" + str);
			tellValidateServiceImpl.addTellCode(tellId, code, new Date().getTime() + 120000);
			Msg msg = getMsg("1", "操作成功!", code);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} catch (HttpErrorException e) {
			// TODO Auto-generated catch block
			Msg msg = getMsg("0", "操作失败!", code);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 
	 * 得到班级验证码
	 * 
	 */
	@RequestMapping(params = { "method=getUserClassCode" })
	public void getUserClassCode(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String code = request.getParameter("code");
		Sys_Class sc = this.tellValidateServiceImpl.getClassCode(code);
		if (sc == null) {
			response.getWriter().write("0");
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(JsonString.sys_class);
			response.getWriter().write(JSONObject.fromObject(sc, jc).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 增加用户
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=addUser")
	public void addUser(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		int temp = this.sysUserImpl.addUser(model);
		if (temp == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else if (temp == 2) {
			Msg msg = this.getMsg("0", "已经存在相同名称或者电话号码，请更换电话号码或者账号名称在进行操作!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();

		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 这里的iNfo,查询他的基本信息
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getStudentBaicInfo")
	public void getStudentBaicInfo(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		SysUser map = this.sysUserImpl.getStudentBaicInfo(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 三方登录
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=loginInfo")
	public void loginInfo(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		SysUser map = this.sysUserImpl.getUserInfo(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 通过手机查询对象
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=byTellInfo")
	public void byTellInfo(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		SysUser map = this.sysUserImpl.byTellInfo(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询当前一级科目
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getByKeMu")
	public void getByKeMu(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		List<Sys_LearnPlant> map = this.sysUserImpl.getByKeMu(model);
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(JsonString.sys_LearnPlant);
		JSONArray jsonArray = JSONArray.fromObject(map, jc);
		response.getWriter().write(jsonArray.toString());
		response.getWriter().flush();
		response.getWriter().close();

	}

	/**
	 * 查询当前人所有班
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getListClassRoom")
	public void getListClassRoom(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		List map = this.sysUserImpl.getListClassRoom(model);
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(JsonString.sys_class);
		JSONArray jsonArray = JSONArray.fromObject(map, jc);
		response.getWriter().write(jsonArray.toString());
		response.getWriter().flush();
		response.getWriter().close();

	}

	/**
	 * 查询当前老師所有的發布的卷
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getListTestPaper")
	public void getListTestPaper(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		Sys_TestPaper st = new Sys_TestPaper();
		Map map = this.sys_TestPaperServiceImpl.getTestPaper(st, model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.test_Paper);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询当前学校每一级的下一级
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getListLv")
	public void getListLv(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String str = request.getParameter("cpid");
		if (null == str || "".equals(str)) {
			str = "0";
		}
		Sys_TestPaper st = new Sys_TestPaper();
		Map map = this.sysUserImpl.getListLv(str);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_class);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询当前科目每一级的下一级
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getListLvKeMu")
	public void getListLvKeMu(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String str = request.getParameter("plpid");
		if (null == str || "".equals(str)) {
			str = "0";
		}
		Map map = this.sysUserImpl.getListLvKeMu(str);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_LearnPlant);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 增加科目
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addLvKemu")
	public void addLvKemu(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

	}

	/**
	 * 删除科目
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=deleteLvkeMu")
	public void deleteLvkeMu(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

	}

	/**
	 * 查询专家标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getZJlable")
	public void getZJlable(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String str = request.getParameter("lablePid");
		if (null == str || "".equals(str)) {
			str = "0";
		}
		Map map = this.sysUserImpl.getZJlable(str);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_lable);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 查询标签，通过labeId
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getLableBylableId")
	public void getLableBylableId(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String str = request.getParameter("lablePid");
		if (null == str || "".equals(str)) {
			str = "0";
		}
		Sys_Lable map = this.sysUserImpl.getLableBylableId(str);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_lable);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 增加标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addLable")
	public void addLable(Sys_Lable model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = new SysUser();
		sysUser.setTable_id(request.getParameter("user_table_id"));
		sysUser.setUserType(request.getParameter("userType"));
		if ("2".equals(sysUser.getUserType()) || "5".equals(sysUser.getUserType())) {
			if ("2".equals(sysUser.getUserType())) {
				model.setType("2");
			} else {
				model.setType("5");
			}
			if (null == model.getLablePid() || "".equals(model.getLablePid())) {
				model.setLablePid("0");
			}
		} else {
			Msg msg = this.getMsg("0", "只要老师和专家才能增加标签!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		model.setSysUser(sysUser);
		// Map map = this.sysUserImpl.getTeacherClass(model);
		Sys_Lable map = this.sysLableService.addSysLable2(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		if (map != null) {

			response.getWriter().write(JSONObject.fromObject(map).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 查询lable所属教材
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getJiaoCai")
	public void getJiaoCai(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		ServletContext application = request.getServletContext();
		List obj = (List) application.getAttribute("jiaocai");
		response.getWriter().write(JSONArray.fromObject(obj).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询个人标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getMyLable")
	public void getMyLable(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		List map = this.sysUserImpl.getMyLable(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_LearnPlant);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	@Autowired
	private ISys_LearnPlant sys_LearnPlantServiceImpl;

	/**
	 * 增加一个备课
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addSys_LearnPlant")
	public void addSys_LearnPlant(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		String user_id = request.getParameter("user_table_id");

		if (null == user_id || "".equals(user_id)) {
			Msg msg = this.getMsg("0", "缺少user_table_id", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		SysUser sysUser = new SysUser();
		sysUser.setTable_id(user_id);
		sysUser.setUserType(request.getParameter("userType"));

		if ("2".equals(sysUser.getUserType()) || "5".equals(sysUser.getUserType())) {
			if ("2".equals(sysUser.getUserType())) {
				model.setLtype("2");
			} else {
				model.setLtype("5");
			}
			if (null == model.getPlpid() || "".equals(model.getPlpid()) || "0".equals(model.getPlpid())) {
				model.setPlpid("0");
				model.setPltype("1");// 第一级
				model.setpName(model.getName());
			} else {
				model.setpName(model.getpName() + "/" + model.getName());// 自己父级名称
			}
		} else {
			Msg msg = this.getMsg("0", "只要老师和专家才能增加备课!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		model.setSysUser(sysUser);
		String arrclass = request.getParameter("arrclass");// 得到所有的关联的班级
		String arrlable = request.getParameter("arrlable");// //得到当前关联的标签

		Sys_LearnPlant map = null;
		String ptable_id = "";
		if ("4".equals(model.getPltype())) {
			// 先装自己的上级存起来
			map = sys_LearnPlantServiceImpl.addSys_LearnPlant(model, arrclass, arrlable);
			if (model.getPltype_1() == null || "".equals(model.getPltype_1())) {
				model.setPltype_1("0");
			}
			// 和题进行关联
			String testpaper = request.getParameter("testpaper");
			if (null != testpaper && !"".equals(testpaper)) {
				Sys_TestPaper st = (Sys_TestPaper) JSONObject.toBean(JSONObject.fromObject(testpaper), Sys_TestPaper.class);
				map.setSys_TestPaper(st);
				this.sys_LearnPlantServiceImpl.updateLearnPlantHomeWork(map);
			}

			// 更新和模板的关联
			String classC = request.getParameter("classC");
			if (null != classC && !"".equals(classC)) {
				Sys_ClassPlantMaster scm = (Sys_ClassPlantMaster) JSONObject.toBean(JSONObject.fromObject(classC), Sys_ClassPlantMaster.class);
				scm.setSys_LearnPlant(map);
				this.sysClassPlantImpl.updatePlantMasterValue(scm);
			}
			// 继续增加自己的下一级
			String pName = map.getpName();
			String plpid = map.getLpid();
			String children = request.getParameter("children");
			ptable_id = map.getTable_id();

			if (!"[]".equals(children) && !"".equals(children)) {
				JSONArray jonArray = JSONArray.fromObject(children);
				if (null != jonArray && null != jonArray.get(0) && !"null".equals(jonArray.get(0)) && !"[]".equals(children)) {
					for (int i = 0; i < jonArray.size(); i++) {
						String jsonstring = jonArray.get(i) + "";
						if ("null".equals(jsonstring)) {
							continue;
						}
						if ("".equals(jsonstring)) {
							continue;
						}
						JSONObject jsonObject = JSONObject.fromObject(jsonstring);
						// lable标签不为空
						String arrleble = "";
						Sys_LearnPlant lp = new Sys_LearnPlant();

						if (null != jsonObject.get("arrlable") && !"".equals(jsonObject.get("arrlable"))) {
							arrleble = jsonObject.get("arrlable").toString();
						}
						// 1是名称
						if (null != jsonObject.get("buzhou1") && !"".equals(jsonObject.get("buzhou1"))) {
							lp.setName(jsonObject.get("buzhou1").toString());
						}
						// 2是时长
						if (null != jsonObject.get("buzhou2") && !"".equals(jsonObject.get("buzhou2"))) {
							lp.setLearntime(jsonObject.getLong("buzhou2"));
						}
						// 3是场地
						if (null != jsonObject.get("buzhou3") && !"".equals(jsonObject.get("buzhou3"))) {
							lp.setAdress(jsonObject.get("buzhou3").toString());
						}
						// 4是附件
						if (null != jsonObject.get("buzhou4") && !"".equals(jsonObject.get("buzhou4"))) {
							// Sys_File sf = new Sys_File();
							// sf.setFilePath(jsonObject.get("buzhou4").toString());
							// if(jsonObject.get("buzhou7")==null ||
							// "".equals(jsonObject.get("buzhou7"))){
							// sf.setType("1");
							// }else{
							// sf.setType(jsonObject.get("buzhou7").toString());
							// }
							// List<Sys_File> listsf = new
							// ArrayList<Sys_File>();
							// listsf.add(sf);
							// lp.setListsf(listsf);

							if (jsonObject.get("buzhou7") != null && !"".equals(jsonObject.get("buzhou7"))) {
								lp.setFujian(jsonObject.get("buzhou4").toString());
								lp.setFujianType(jsonObject.get("buzhou7").toString());
							}
						}
						// 5是内容
						if (null != jsonObject.get("buzhou5") && !"".equals(jsonObject.get("buzhou5"))) {
							lp.setContent(jsonObject.get("buzhou5").toString());
						}
						// 6是器材
						if (null != jsonObject.get("buzhou6") && !"".equals(jsonObject.get("buzhou6"))) {
							lp.setJiaoju(jsonObject.get("buzhou6").toString());
						}
						lp.setPlpid(plpid);
						lp.setPltype("5");
						lp.setLtype(sysUser.getUserType());
						lp.setpName(pName + "/" + lp.getName());
						lp.setId1(map.getId1());
						lp.setId2(map.getId2());
						lp.setId3(map.getId3());
						lp.setId4(map.getId4());
						lp.setSysUser(sysUser);
						map = sys_LearnPlantServiceImpl.addSys_LearnPlant(lp, arrclass, arrleble);
					}
				}
			}
		} else {
			map = sys_LearnPlantServiceImpl.addSys_LearnPlant(model, arrclass, arrlable);
		}
		if (null != map) {
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(JsonString.sys_LearnPlant);
			// Msg msg = this.getMsg("0", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(map, jc).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 查询下一级所有的步骤
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getBuzhou")
	public void getBuzhou(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		if ("-1".equals(model.getTable_id()) || "-1".equals(model.getLpid())) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		JSONArray map = this.sys_LearnPlantServiceImpl.getLearnPlantChildren(model);
		response.getWriter().write(map.toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询下一级所有的步骤
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getBeikeJuan")
	public void getBeikeJuan(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		JSONArray map = this.sys_LearnPlantServiceImpl.getBeikeJuan(model);
		response.getWriter().write(map.toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询下一级所有的标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getThisLable")
	public void getThisLable(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		if ("-1".equals(model.getTable_id()) || "-1".equals(model.getLpid())) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Map map = this.sys_LearnPlantServiceImpl.getLearnPlantLable(model);
		response.getWriter().write(JSONObject.fromObject(map).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询当前最上级的树下面，总共所有的标签
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=deletelearnPlant")
	public void deletelearnPlant(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		int map = this.sys_LearnPlantServiceImpl.deletelearnPlant(model);
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 查询我的教室
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getMyshangkeClass")
	public void getMyshangkeClass(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		model.setTable_id(request.getParameter("user_table_id"));
		if (model == null || model.getTable_id() == null || "".equals(model.getTable_id())) {
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Map map = this.sysUserImpl.getTeacherClass(model);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_user);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 规划我的教室
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=guihuaJiaoshi")
	public void guihuaJiaoshi(Sys_Room model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		response.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		SysUser sysUser = new SysUser();
		sysUser.setTable_id(request.getParameter("user_table_id"));
		String sys_Class_table_id = request.getParameter("sys_Class_table_id");
		if (sysUser.getTable_id() == null || "".equals(sysUser.getTable_id())) {
			System.out.println("操作失败!没有找到对应的老师");
			Msg msg = this.getMsg("0", "操作失败!没有找到对应的老师", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if (null == sys_Class_table_id || "".equals(sys_Class_table_id)) {
			System.out.println("操作失败!没有找到对应的班级");
			Msg msg = this.getMsg("0", "操作失败!没有找到对应的班级", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Sys_Class sc = new Sys_Class();
		sc.setTable_id(sys_Class_table_id);
		model.setSys_Class(sc);
		model.setSysUser(sysUser);
		String arr = request.getParameter("arr");
		if (null == arr || "".equals(arr)) {
			Msg msg = this.getMsg("0", "操作失败，教室中没有座位!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		int map = this.sysClassServiceImpl.addClass_Room(model, arr);
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 查询我的教室位置
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=jiaoshiWeiZhi")
	public void jiaoshiWeiZhi(Sys_Room model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		SysUser sysUser = new SysUser();
		sysUser.setTable_id(request.getParameter("user_table_id"));
		String sys_Class_table_id = request.getParameter("sys_Class_table_id");
		String year = request.getParameter("year");
		// 由于这的session就是老师了，所以在每个参数中，直接传入老师就可以，
		Map sr = this.sysClassServiceImpl.getStudent_Teacher_Room(sysUser, sysUser, sys_Class_table_id, year);

		if (sr == null) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		if (sr.get("sys_Room_Position_User") == null || "null".equals(sr.get("sys_Room_Position_User")) || sr.get("sys_Room") == null || "null".equals(sr.get("sys_Room"))) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(JsonString.sys_room);
		JSONObject jonArray = JSONObject.fromObject(sr, jc);
		response.getWriter().write(jonArray.toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 查询老师的已经的课排表
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTeacherPlantMaster")
	public void getTeacherPlantMaster(SysUser model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		if (model == null || null == model.getTable_id() || "".equals(model.getTable_id())) {
			System.out.println("-------------课排表没有老师ID---------------");
			response.getWriter().write("{}");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Map map = this.sysClassPlantImpl.getTeacherPlantMaster(model);
		if ("null".equals(map.get("list")) || map.get("list") == null) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_classPlantMaster);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 增加卷
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=addTestPaper")
	public void addTestPaper(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String timepicker1 = request.getParameter("timepicker1");
		String timepicker2 = request.getParameter("timepicker2");
		// model.setShowStartTime(model.getShowStartTime() + " " + timepicker1);
		// model.setShowEndTime(model.getShowEndTime() + " " + timepicker2);
		model.setShowStartTime(Common.getTime());
		model.setShowEndTime(Common.getTime());
		SysUser sysUser = new SysUser();
		String user_table_id = request.getParameter("user_table_id");
		if (null == user_table_id || "".equals(user_table_id)) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		sysUser.setTable_id(request.getParameter("user_table_id"));
		model.setSysUser(sysUser);
		int temp = 0;
		if (null != model.getTable_id() && !"".equals(model.getTable_id())) {
			temp = sys_TestPaperServiceImpl.updateTestPaper(model);
		} else {
			model = sys_TestPaperServiceImpl.addTestPaper1(model);
		}
		if (model != null) {
			JsonConfig jc = new JsonConfig();
			jc.setExcludes(JsonString.test_Paper);
			response.getWriter().write(JSONObject.fromObject(model, jc).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 增加卷中题
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=addTestPaperQuestion")
	public void addTestPaperQuestion(Sys_Question model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		String juanId = request.getParameter("juanId");
		if (null == juanId || "".equals(juanId)) {
			Msg msg = this.getMsg("0", "没有发现卷Id!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Sys_TestPaper st = new Sys_TestPaper();
		st.setTable_id(juanId);
		model.setSys_TestPaper(st);
		int temp = 0;
		if (null != model.getTable_id() && !"".equals(model.getTable_id())) {
			temp = sys_TestPaperServiceImpl.updateTestPaperQuestion(model);
		} else {
			model = sys_TestPaperServiceImpl.addTestPaperQuestion1(model);
		}
		if (temp == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 增加卷中题
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getTestPapterQuestion")
	public void getTestPapterQuestion(Sys_TestPaper model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Map map = this.sys_TestPaperServiceImpl.getTestPaperQuestion(model);
		if (null == map.get("list") || "".equals(map.get("null"))) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(JsonString.sys_question);
		response.getWriter().write(JSONObject.fromObject(map, jc).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 增加备课中的作业的中的题
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getLaranPlantTestPapterQuestion")
	public void getLaranPlantTestPapterQuestion(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		Sys_LearnPlant sl = this.sys_LearnPlantServiceImpl.getSys_LearnPlantById(model);
		if (sl == null || sl.getSys_TestPaper() == null) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Sys_TestPaper st = sl.getSys_TestPaper();
		Map map = this.sys_TestPaperServiceImpl.getTestPaperQuestion(st);
		if (null == map.get("list") || "".equals(map.get("null"))) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(JsonString.sys_question);
		response.getWriter().write(JSONObject.fromObject(map, jc).toString());
		response.getWriter().close();
		response.getWriter().flush();
	}

	/**
	 * 增加备课中的作业的中的题
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=getShangkeLearn")
	public void getShangkeLearn(Sys_LearnPlant model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String str = request.getParameter("user_table_id");
		if ("".equals(str) || null == str) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		SysUser sysUser = new SysUser();
		sysUser.setTable_id(str);

		Map map = this.sys_LearnPlantServiceImpl.getClassBegin(sysUser);
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		// jsonConfig.setExcludes(JsonString.sys_LearnPlant);
		System.out.println(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 加分
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addStudentFen")
	public void addStudentFen(Sys_Room model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String str1 = request.getParameter("arrLable");
		String str2 = request.getParameter("arrStudent");
		String str3 = request.getParameter("fen");
		if ("".equals(str1) || null == str1 || "".equals(str2) || null == str2) {
			response.getWriter().write("-1");
			response.getWriter().close();
			response.getWriter().flush();
		}
		List<SysUser> listu = (List<SysUser>) JSONArray.toCollection(JSONArray.fromObject(str2), SysUser.class);
		List<Sys_Lable> listl = (List<Sys_Lable>) JSONArray.toCollection(JSONArray.fromObject(str1), Sys_Lable.class);
		int fen = 0;
		if (null == str3 || "".equals(str3)) {
			fen = 0;
		} else {
			fen = Integer.parseInt(str3);
		}
		int temp = this.sysUserImpl.addStudentFen(listu, listl, fen);
		Msg msg = this.getMsg("1", "操作成功!", null);
		response.getWriter().write(JSONObject.fromObject(msg).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}
}
